//JavaScript代码区域
layui.use(['layer', 'form', 'table', 'upload'], function() {
	var layer = layui.layer,
		$ = layui.$,
		form = layui.form,
		upload = layui.upload,
		table = layui.table;
	//监听提交
	form.on('submit(formDemo)', function(data) {
		table.reload('idTest', {
			where: data.field
		});
		return false;
	});
	//执行实例（excel导入）
	var importExcel = upload.render({
		elem: '#importExcel',
		url: IP+'/api/teacher_eav/sysStudent/impExcel',
		accept: 'file',
		acceptMime: 'file/xls,file/xlsx',
		exts: 'xls|xlsx',
		auto: false,
		bindAction: '#importExcelBtn',
		before: function(obj){
		    $('#upDiv .tishi1').show()
		},
		done: function(res) {
			layer.closeAll();
		    $('#upDiv .tishi1').hide();
			layer.msg(res.message, {
				icon: 1,
				time: 4000
			});
		},
		error: function() {
			layer.closeAll("loading");
		    $('#upDiv .tishi1').hide();
			//请求异常回调
		},
		choose: function(obj) {
			var files = obj.pushFile();
			obj.preview(function(index, file, result) {
				$('#upDiv .tishi').html("你已选择："+file.name)
			});
			//页面层-自定义
			layer.open({
			  type: 1,
			  title: false,
			  content: $("#upDiv")
			});
		}
	});
	//数据表格实例
	table.render({
		id: 'idTest',
		elem: '#demo',
		url: IP + '/api/teacher_eav/sysStudent',
		height: 'full-180',
		method: 'get',
		page: true,
		even: true,
		cols: [
			[{
				type: 'checkbox'
			}, {
				field: 'sId',
				width: 100,
				title: '学生编号',
				sort: true,
				hide: true
			}, {
				field: 'sNo',
				width: 100,
				title: '学生学号',
				sort: true
			}, {
				field: 'sName',
				title: '学生姓名'
			}, {
				field: 'sSex',
				title: '性别'
			}, {
				field: 'sType',
				title: '学生类型'
			}, {
				field: 'classId',
				title: '班级'
			}, {
				field: 'createTime',
				title: '创建时间',
				sort: true
			}, {
				field: 'updateTime',
				title: '修改时间',
				sort: true
			}, {
				fixed: 'right',
				width: 200,
				title: '操作',
				align: 'center',
				toolbar: '#barDemo'
			}]
		],

		request: {
			pageName: 'page',
			limitName: 'pageSize'
		},
		response: {
			statusName: 'code',
			statusCode: 200,
			msgName: 'msg',
			countName: 'count',
			dataName: 'data'
		},
		done: function(res, curr, count) {
			layer.closeAll('loading');
			refreshTableDict();
		}
	});
	//数据表格监听工具条(查看、编辑、删除按钮)
	table.on('tool(test)', function(obj) {
		var data = obj.data;
		var layEvent = obj.event;
		if(layEvent === 'del') {
			var ts = data.sId;
			var uids = [];
			uids.push(data.sId)
			deleteByUids(ts, uids);
		} else if(layEvent === 'edit') {
			window.location = "student-update.html?sId=" + data.sId;
		}
	});
	//复选框批量操作
	table.on('checkbox(test)', function(obj) {
		var checkStatus = table.checkStatus('idTest');
		if(checkStatus.data.length > 0) {
			$('#deleteall').show()
		} else {
			$('#deleteall').hide()
		}
	});
	$(function() {
		//批量删除按钮
		$('#deleteall').click(function() {
			var checkStatus = table.checkStatus('idTest');
			if(checkStatus.data.length > 0) {
				var list = checkStatus.data;
				var uids = [];
				var ts = "";
				$.each(list, function(idx, obj) {
					uids.push(obj.sId)
					ts += " " + obj.sId
				});
				deleteByUids(ts, uids);
			}
		})
		//添加按钮事件
		$('#addstudent').on('click', function() {
			window.location = 'student-add.html?type=add'
		})
	})
	//增加
	function add() {

	}
	//单、批量删除
	function deleteByUids(ts, uids) {
		layer.confirm('确认删除编号:' + ts + "？", {
			icon: 3
		}, function(index) {
			layer.close(index);
			$.ajax({
				url: IP + '/api/teacher_eav/sysStudent',
				type: "DELETE",
				data: JSON.stringify(uids),
				success: function(result, status, xhr) {
					layer.closeAll('loading');
					layer.msg(result.message, {
						icon: 1,
						time: 1000
					}, function() {
						table.reload('idTest', {});
					});
				}
			});
		});
	}
	//更新表格字典信息
	function refreshTableDict() {
		var table_datas = $('#demo').siblings('.layui-form').children('.layui-table-box')
			.children('.layui-table-body').children('.layui-table').children('tbody').children('tr');

		$.each(table_datas, function(idx, obj) {
			//类别
			var sSex = $(this).children("td[data-field='sSex']").children('div');
			for(var i = 0; i < sSex.length; i++) {
				if(sSex.html() == "1") {
					sSex.html("男");
					break;
				} else {
					sSex.html("女");
					break;
				}
			}

			var sType = $(this).children("td[data-field='sType']").children('div');
			for(var i = 0; i < sType.length; i++) {
				if(sType.html() == "1") {
					sType.html("在校");
					break;
				} else if(sType.html() == "2") {
					sType.html("退学");
					break;
				} else {
					sType.html("休学");
					break;
				}
			}
		});
	}
	//查询信息
	function ViewUserDetail(uid) {
		layer.open({
			type: 2,
			skin: 'layui-layer-molv',
			area: ['600px', '340px'],
			maxmin: true,
			content: 'student-query.html?sId=' + sId
		});
	}
	//数据下载
	$("#exportExcel").click(function(){
		var requestUrl =IP+ "/api/teacher_eav/sysStudent/exportExcel?classId="+$("#classId").val().trim();
    	var link_= $('<a href="'+requestUrl+'" target="_blank"></a>');
    	link_.get(0).click();
	})
});